<?php

namespace app\controller\wmapi;

use Illuminate\Support\Facades;
use Illuminate\Support\Facades\Validator;
use plugin\admin\app\common\Util;
use plugin\admin\app\model\Admin;
use support\Request;
use app\model\WaAdmins;
use app\model\WaRoles;
use app\model\WaRules;
use app\model\WaDepartment;
use app\model\WaAdminRoles;
use Illuminate\Support\Facades\DB;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Cache;
use Tinywan\Validate\Validate;

class RoleController extends Validate
{
    /**
     * 角色管理 -列表-分页
     * @param Request $request
     * @return mixed
     */
    public function roleList(Request $request)
    {
        $param = $request->all();
        $fields = ['roles.*', 'admin.username as parent_name'];
        $condition = [];
        if ($param['name']) {
            $condition [] = ['roles.name', 'like', '%' . $param['name'] . '%'];
        }
        $page = page($param['page']);
        $pageNum = pageNum($param['pageNum']);
        $rule = WaRoles::list($page, $pageNum, $fields, $condition);

        return IndexController::successed($rule, '角色列表');
    }

    /**
     * 角色管理 -列表-不分页
     * @param Request $request
     * @return mixed
     */
    public function roleAll(Request $request)
    {
//        process($request);
        $param = $request->all();
        $fields = ['roles.*', 'admin.username as parent_name'];
        $condition = [];
        $rule = WaRoles::all($fields, $condition);
        return IndexController::successed($rule, '角色列表');
    }


    /**
     * 角色-创建
     * @param Request $request
     * @return \support\Response
     */
    public function roleCreate(Request $request)
    {
//        process($request);
        $param = $request->all();
        $adminId = json_decode(adminId($request));
        /**
         * 验证规则
         */
        $rule = [
            'name' => 'require|max:50', // 用户名
        ];
        if ($param['rules']){
            $param['rules'] = json_encode($param['rules']);
        }
        $data = [
            'name' => $param['name'] ? $param['name'] : '', // 用户名
            'rules' => $param['rules'], // 权限
            'description' => $param['description'] ? $param['description'] : '', // 描述
            'created_at' => date('Y-m-d H:i:s', time()), // 创建时间
            'status' => 0, // 1 禁用
            'admin_id' => $adminId->id,
        ];

//        $validate = \Tinywan\Validate\Facade\Validate::rule($rule);
//        if (!$validate->check($param)) {
//            return IndexController::errored($validate->getError());
//        } else {
            $where = ['name' => $param['name']];
            $find = WaRoles::find($where);
            $find = json_decode($find);

            if ($find){
                if (property_exists($find, 'id')) {
                    return IndexController::errored('', '角色创建失败,角色名称已存在');
                }
            }
            $res = WaRoles::created($data);
            if (!$res) {
                return IndexController::errored('', '角色创建失败');
            } else {
                return IndexController::successed('', '角色创建成功');
            }
//        }

    }

    /**
     * 角色-更新
     * @param Request $request
     * @return \support\Response
     */
    public function roleEdit(Request $request)
    {
//        process($request);
        $param = $request->all();
        /**
         * 验证规则
         */
        $rule = [
            'name' => 'require|max:50', // 用户名
            'role_id' => 'require', // 角色id
        ];
        if ($param['rules']){
            $param['rules'] = json_encode($param['rules']);
        }
        $data = [
            'name' => $param['name'] ? $param['name'] : '', // 用户名
            'rules' => $param['rules'], // 权限
            'description' => $param['description'] ? $param['description'] : '', // 描述
            'updated_at' => date('Y-m-d H:i:s', time()), // 创建时间
//            'status' => $param['status'], // 1 禁用
        ];

//        $validate = \Tinywan\Validate\Facade\Validate::rule($rule);
//        if (!$validate->check($param)) {
//            return IndexController::errored($validate->getError());
//        } else {
            $where = ['name' => $param['name']];
            $find = WaRoles::find($where);
            $find = json_decode($find);
            if ($find){
                if (property_exists($find, 'id')) {
                    if ($param['name'] != $find->name) {
                        return IndexController::errored('', '角色更新失败,用户名已存在');
                    }
                }
            }

            $condition = [];
            $condition [] = ['id', '=', intval($param['role_id'])];
            $res = WaRoles::updated($condition, $data);
            if (!$res) {
                return IndexController::errored($res, '角色更新失败');
            } else {
                return IndexController::successed($res, '角色更新成功');
            }
//        }
    }

    /**
     * 角色-删除
     * @param Request $request
     * @return \support\Response
     */
    public function roleDel(Request $request)
    {
//        process($request);
        $param = $request->all();
        $condition = ['id' => $param['role_id']];
        $fields = ['*'];
        $conditionAdmin = [];
        $admins = WaAdmins::all($fields,$conditionAdmin);
        if ($admins){
            $admins = json_decode($admins);
            foreach ($admins as $k){
                if ($k->role_id){
                    if (in_array($k->role_id, json_decode($k->role_id))){
                        return IndexController::errored($k->id,'该角色下有绑定用户无法删除该角色');
                    }
                }
            }
        }
        $rule = WaRoles::deleted($condition);
        return IndexController::successed($rule, '角色-删除成功');
    }


    public function roleDetail(Request $request){
//        process($request);
        $param = $request->all();
        $condition = [];
        $condition [] = ['id', '=', $param['role_id']];
        $data = []; // 1禁用 其它默认正常显示
        $rule = WaRoles::find($condition, $data);
        $rule = json_decode($rule);
        if ($rule){
            $rule->rules = json_decode($rule->rules);
        }
        $rule = json_encode($rule);
        return IndexController::successed($rule, '角色信息');
    }


}